package com.hframe.typefilter;

import org.apache.log4j.Logger;

import java.text.SimpleDateFormat;

public class DateToString implements FilterRef.Date<Object, java.util.Date> {
    private static final Logger log = Logger.getLogger(DateToString.class);
    private String format = DATE_TIME_LINE;

    public DateToString() {
    }

    /**
     * 时间格式
     *
     * @param format
     */
    public DateToString(String format) {
        this.format = format;
    }

    @Override
    public Object filter(java.util.Date object) {
        if (null == object)
            return null;
        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            return formater.format(object);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }

    public static String format(java.util.Date date, String format) {
        if (null == date)
            return null;

        SimpleDateFormat formater = new SimpleDateFormat(format);
        try {
            return formater.format(date);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
}